let mysql = require("mysql");
require("dotenv").config();
const pool = mysql.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PWD,
  database: process.env.DB_DATABASE,
  port: process.env.DB_PORT,
});
const exec = (sql, params) => {
  return new Promise((resolve, reject) => {
    pool.getConnection(function (err, conn) {
      if (err) {
        console.log("连接数据库失败:" + err.message);
        reject(err);
        return;
      }
      conn.query(sql, params, function (err, result) {
        if (err) {
          console.log("执行sql语句失败:" + err.message);
          reject(err);
          return;
        }
        resolve(result);
      });
      conn.release();
    });
  });
};
module.exports = { exec };
